﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Orchard.ContentManagement;
using Orchard.ContentManagement.MetaData;
using Orchard.ContentManagement.MetaData.Builders;
using Orchard.ContentManagement.MetaData.Models;
using Orchard.ContentManagement.ViewModels;
using Orchard.Media.Services;

namespace Szmyd.Orchard.Modules.Silverlight.Settings
{
    /// <summary>
    /// Content type settings for SilverlightPart.
    /// </summary>
    public class SilverlightPartTypePartSettings
    {
        /// <summary>
        /// The default source file.
        /// </summary>
        public string DefaultSource { get; set; }

        /// <summary>
        /// The default initialization parameters.
        /// </summary>
        public string DefaultInitParams { get; set; }

        [HiddenInput(DisplayValue = false)]
        public IEnumerable<object> AvailableFiles { get; set; }
    }

    /// <summary>
    /// Overrides default editors to enable putting settings on Silverlight part.
    /// </summary>
    public class SilverlightPartSettingsHooks : ContentDefinitionEditorEventsBase
    {
        private readonly IMediaService _mediaService;

        public SilverlightPartSettingsHooks(IMediaService mediaService)
        {
            _mediaService = mediaService;
        }

        /// <summary>
        /// Overrides editor shown when part is attached to content type. Enables adding setting field to the content part
        /// attached.
        /// </summary>
        /// <param name="definition"></param>
        /// <returns></returns>
        public override IEnumerable<TemplateViewModel> TypePartEditor(ContentTypePartDefinition definition)
        {
            if (definition.PartDefinition.Name != "SilverlightPart")
                yield break;
            var model = definition.Settings.GetModel<SilverlightPartTypePartSettings>();
            var files = _mediaService
                .GetMediaFiles("Silverlight")
                .Where(f => f.Type == ".xap")
                .Select(f => (dynamic)new
                {
                    Path = _mediaService.GetPublicUrl("Silverlight/" + f.Name),
                    f.Name
                }
                )
                .ToList();

            model.AvailableFiles = new[] {
                (dynamic)new 
                {
                    Path = "",
                    Name = "<None>"
                }
            }.Concat(files);
            yield return DefinitionTemplate(model);
        }

        public override IEnumerable<TemplateViewModel> TypePartEditorUpdate(ContentTypePartDefinitionBuilder builder, IUpdateModel updateModel)
        {
            if (builder.Name != "SilverlightPart")
                yield break;

            var model = new SilverlightPartTypePartSettings();

            updateModel.TryUpdateModel(model, "SilverlightPartTypePartSettings", null, null);
            //if (!string.IsNullOrWhiteSpace(model.DefaultSource))
            builder.WithSetting("SilverlightPartTypePartSettings.DefaultSource", model.DefaultSource);
            //if (!string.IsNullOrWhiteSpace(model.DefaultInitParams))
            builder.WithSetting("SilverlightPartTypePartSettings.DefaultInitParams", model.DefaultInitParams);
            yield return DefinitionTemplate(model);
        }

    }
}
